package com.net.xpay.common.manager.impl.sync;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.sync.CommonPayTransBatchManager;
import com.net.common.annotation.ReadTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.sync.CommonPayTransBatchMapper;
import com.net.xpay.common.domain.sync.CommonPayTransBatch;
import com.net.xpay.common.model.TransStatSumDataModel;
import com.net.xpay.core.model.PayTransBatchSearchModel;
import com.net.xpay.common.model.TransStatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
@ReadTransactional
public class CommonPayTransBatchManagerImpl extends AbstractManager<CommonPayTransBatch> implements CommonPayTransBatchManager {

    @Autowired
    private CommonPayTransBatchMapper mapper;


    @Override
    protected BasicMapper<Long, CommonPayTransBatch> getMapper() {
        return mapper;
    }

    @Override
    public int save(CommonPayTransBatch obj) {
        return getMapper().insert(obj);
    }

    @Override
    public int update(CommonPayTransBatch obj) {
        Preconditions.checkArgument(obj != null && obj.getId() != null);
        return getMapper().updateByPrimaryKey(obj);
    }

    @Override
    public List<CommonPayTransBatch> search(PayTransBatchSearchModel searchModel) {
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(PayTransBatchSearchModel searchModel) {
        return mapper.searchCount(searchModel);
    }

    @Override
    public TransStatSumDataModel sumAccumulatedData() {
        TransStatSumDataModel data = new TransStatSumDataModel();
        //累计总付款支付宝数量
        long totalPaidAlipayUserCount = mapper.sumTotalPaidAlipayUserCount(null, null);

        data.setTotalPaidAlipayUserCount(totalPaidAlipayUserCount);

        //累计超过500w金额支付宝数量
        long totalVeryAlipayUserCount = mapper.sumTotalVeryAlipayUserCount();
        data.setTotalVeryAlipayUserCount(totalVeryAlipayUserCount);

        //累计收款支付宝数量
        long totalReceiveAlipayCount = mapper.sumTotalReceiveAlipayCount();
        data.setTotalReceiveAlipayCount(totalReceiveAlipayCount);

        return data;
    }

    @Override
    public TransStatModel sumTransStat(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(minEndTime != null && maxEndTime != null);
        return mapper.sumTransStat(minEndTime, maxEndTime);
    }

    @Override
    public List<Long> listTotalVeryMerchantIds() {
        List<Long> rets = mapper.listTotalVeryMerchantIds();
        return rets == null ? new ArrayList<>() : rets;
    }

    @Override
    public long statPayTransActivePartnerCount(LocalDateTime minEndTime, LocalDateTime maxEndTime) {

        return mapper.statPayTransActivePartnerCount(minEndTime, maxEndTime);
    }

    @Override
    public long sumTotalPaidAlipayUserCount(LocalDateTime startTime, LocalDateTime endTime) {
        return mapper.sumTotalPaidAlipayUserCount(startTime, endTime);
    }

}
